<?php
/**
 * 整体妆容素材 产品 多对多关联表
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/6/13
 * @copyright PanxSoft Inc.
 */
namespace App\Models\Customer;

use App\Models\Panx\Product;
use App\Models\Panx\ProductTag;
use App\Models\Store\Pack;
use App\Observers\BosCleanObserver;
use App\Services\Utils\Bosable;
use DemeterChain\A;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;

/**
 * @property int id
 * @property int category_id
 * @property int product_id
 * @property int pack_id
 *
 * @property-read Product|null product - 数据库中有对应的商品被删除的情况
 * @property-read Asset asset
 * @property-read Pack pack
 *
 * @mixin Builder
 */
class PackAssetPivotProduct extends Model
{
    protected static $customer_cache = [
        // example
        'customer_id' => [ // <- Eloquent Collection|self[]
            'self 1 with -> product',
            'self 2 with -> product'
        ]
    ];

    protected $table = 'pack_asset_pivot_product';

    protected $guarded = [];

    protected $casts = [
        'product_id' => 'int',
        'pack_id'    => 'int',
        'asset_id'   => 'int',
    ];

    /**
     * @used-by $product
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id', 'id', 'product');
    }

    /**
     * @used-by $asset
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function asset()
    {
        return $this->belongsTo(Asset::class, 'asset_id', 'id');
    }

    /**
     * @used-by $pack
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function pack()
    {
        return $this->belongsTo(Pack::class, 'pack_id', 'id');
    }

    /**
     * @param int $customer_id
     *
     * @return void
     */
    public function cacheCustomerPackAssetProducts(int $customer_id): void
    {
        $pack_ids = (new Pack)
            ->where(compact('customer_id'))
            ->pluck('id')
            ->all();

        $pack_asset_pvt_products = (new PackAssetPivotProduct)
            ->whereIn('pack_id', $pack_ids)
            ->with(['product.thumbImage'])
            ->get();

        PackAssetPivotProduct::$customer_cache[$customer_id] = $pack_asset_pvt_products;
    }

    /**
     * @param int $customer_id
     *
     * @return Collection
     */
    public function getCustomerCache(int $customer_id): Collection
    {
        if (! isset(self::$customer_cache[$customer_id])) {
            $this->cacheCustomerPackAssetProducts($customer_id);
        }

        return self::$customer_cache[$customer_id];
    }

    /**
     * @param int $product_id
     *
     * @return bool|null
     * @throws \Exception
     */
    static public function cleanDeleteProduct(int $product_id)
    {
        return (new self)
            ->where(compact('product_id'))
            ->delete();
    }
}
